#include "widget.h"
#include "ui_widget.h"
QMutex  myMutex;
Widget::Widget(QWidget *parent)
    : QWidget(parent) , ui(new Ui::Widget)
{
    ui->setupUi(this);
    for(int i = 0 ; i < 3; ++i){
     producerVector.push_back(new ProducerThread(i+1,&hasSpaceCond,&hasDataCond));
    }

    for(int i = 0 ; i < 2; ++i){
     customerVector.push_back(new CustomerThread(i+1,&hasSpaceCond,&hasDataCond));
    }

}

Widget::~Widget()
{
    auto it = customerVector.begin();
    for(;it != customerVector.end() ; ++it){
        (*it)->setStop();
    }

    auto it0 = producerVector.begin();
    for(;it0 != producerVector.end() ; ++it0){
        (*it0)->setStop();
    }


    auto it2 = customerVector.begin();
    for(;it2 != customerVector.end() ; ++it2){
        (*it2)->wait();
    }

    auto it3 = producerVector.begin();
    for(;it3 != producerVector.end() ; ++it3){
        (*it3)->wait();
    }


    qDeleteAll(producerVector.begin(),producerVector.end());
    qDeleteAll(customerVector.begin(),customerVector.end());
    delete ui;
}


void Widget::on_pushButton_clicked()
{

//    auto it = customerVector.begin();
//    for(;it != customerVector.end() ; ++it){
//        (*it)->start();
//    }

    auto it0 = producerVector.begin();
    for(;it0 != producerVector.end() ; ++it0){
        (*it0)->start();
    }

}
